;(function (global, document, undefined) {
    "use strict";
    var _global;
    var tchUpload = {
        defaults: {
            title: '头像上传',
            skin: '',
            style: '',
            upload_icon: '',
            left_icon: '',
            right_icon: '',
            big_icon: '',
            small_icon: '',
            save_icon: '',
            close_icon: '',
            onsave: null,
            onclose: null,
            onerror: null
        },
        options: {
            resImgWigth: 0,
            resImgHeight: 0,
            img: null,
            imgView: null,
            canvas: null,
            sx: 0,
            sy: 0,
            draw2d: null,
            drawView2d: null,
            width: 0,
            height: 0,
            x: 0,
            y: 0,
            isDown: false,
            mouseX: 0,
            mouseY: 0,
            rotate: 0,
            rotateNew: 0,
            scale: 1,
            scaleNew: 1
        },
        open: function (paras) {
            if (!paras) paras = [];
            for (var name in this.defaults) {
                if (paras[name]) {
                    this.defaults[name] = paras[name]
                }
            }
            var _css = '<style>body{overflow:hidden}.tintcloud-img-background{width:100%;height:100%;overflow:hidden;position:fixed;top:0;left:0;background:#666;opacity:.5;z-index:9996}.tintcloud-img{background:#fff;width:500px;border:1px solid #ccc;border-radius:3px;box-shadow:5px 5px 10px #aaa;z-index:9997;position:fixed;top:calc(50% - 158px);left:calc(50% - 250px)}.tintcloud-img .tc-img-title{width:calc(100% - 20px);height:28px;padding:5px 10px 0;border-bottom:1px solid #ccc}.tintcloud-img .tc-img-title div{float:left}.tintcloud-img .tc-img-title .tc-img-title-text{color:#666;font-size:15px}.tintcloud-img .tc-img-title .tc-img-title-close{color:#666;float:right;width:22px;height:22px;text-align:center;cursor:pointer;border-radius:3px}.tintcloud-img .tc-img-title .tc-img-title-close:hover{color:#fff;background:red}.tintcloud-img .tc-img-content{width:100%;height:280px}.tintcloud-img .tc-img-content div{float:left}.tintcloud-img .tc-img-content .tc-img-content-select{width:260px;height:260px;padding:10px}.tintcloud-img .tc-img-content .tc-img-content-select .tc-img-canvas{width:258px;height:258px;border:1px solid #ccc;opacity:.9;background-image:url()}.tintcloud-img .tc-img-content .tc-img-content-select .tc-img-canvas #tcImgCanvas{width:258px;height:258px;overflow:hidden;position:relative;z-index:9998}.tintcloud-img .tc-img-content .tc-img-content-select .tc-img-canvas #tcImgBox{width:100%;height:100%;background:#000;opacity:.5;top:0;left:0;position:absolute;z-index:9999}.tintcloud-img .tc-img-content .tc-img-content-select .tc-img-canvas #tcImgCanvas #tcImgSelect{position:absolute;border:#69f 1px solid;cursor:move;z-index:10000}.tintcloud-img .tc-img-content .tc-img-content-form{width:calc(100% - 300px);height:258px;padding:11px 10px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button{text-decoration:none;text-align:center;border-radius:3px;font-size:14px;display:block}.tintcloud-img .tc-img-content .tc-img-content-form .form-upload{width:calc(100% - 60px);height:24px;background:#6cf;color:#fff;padding-top:6px;padding-left:60px}.tintcloud-img .tc-img-content .tc-img-content-form .form-upload img{width:18px;height:18px;float:left}.tintcloud-img .tc-img-content .tc-img-content-form .form-upload span{margin-left:5px;float:left;margin-top:-1px}/*by tintcloud.com*/.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2,.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4{width:100%;margin-top:10px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 a{width:50%;float:left;height:24px;padding-top:6px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 a img{width:18px;height:18px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#99c;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 .form-right{background:#9cc;color:#fff;border-radius:0}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 .form-magnification{background:#cc9;color:#fff;border-radius:0}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#c99;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2 a{width:calc(50% - 25px);float:left;height:24px;padding-top:6px;padding-left:25px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2 a img{width:18px;height:18px;float:left}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2 a span{margin-left:5px;float:left;margin-top:-1px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#9c6;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img .tc-img-content .tc-img-content-form .form-display{width:100%;height:138px;margin-top:10px;text-align:center}.tintcloud-img .tc-img-content .tc-img-content-form .form-display .form-display-panel{width:137px;height:137px;margin-left:30px;border:1px solid #ccc}.tintcloud-img .tc-img-content .tc-img-content-form .form-display img{width:137px;height:137px}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-upload{background:#888}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#aaa;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group4 .form-right{background:#bbb;color:#fff;border-radius:0}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group4 .form-magnification{background:#aaa;color:#fff;border-radius:0}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#bbb;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#9c6;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-grey .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-upload{background:#69c}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#69f;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group4 .form-right{background:#80a9fa;color:#fff;border-radius:0}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group4 .form-magnification{background:#69f;color:#fff;border-radius:0}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#80a9fa;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#69c;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-blue .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-upload{background:#f96}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#f99;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group4 .form-right{background:#f9c;color:#fff;border-radius:0}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group4 .form-magnification{background:#f99;color:#fff;border-radius:0}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#f9c;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#f96;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-red .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-upload{background:#6c9}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#9c9;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group4 .form-right{background:#9cc;color:#fff;border-radius:0}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group4 .form-magnification{background:#9c9;color:#fff;border-radius:0}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#9cc;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#6c9;color:#fff;border-radius:3px 0 0 3px}.tintcloud-img-green .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0 3px 3px 0}.tintcloud-img-metro,.tintcloud-img-metro .tc-img-content .tc-img-content-form .form-button,.tintcloud-img-metro .tc-img-title .tc-img-title-close{border-radius:0}.tintcloud-img-metro .tc-img-content .tc-img-content-form .form-button-group4 .form-left{background:#99c;color:#fff;border-radius:0}.tintcloud-img-metro .tc-img-content .tc-img-content-form .form-button-group4 .form-shrink{background:#c99;color:#fff;border-radius:0}.tintcloud-img-metro .tc-img-content .tc-img-content-form .form-button-group2 .form-save{background:#9c6;color:#fff;border-radius:0}.tintcloud-img-metro .tc-img-content .tc-img-content-form .form-button-group2 .form-close{background:#f66;color:#fff;border-radius:0}</style>';
            var _html = _css + '<div class="tintcloud-img-background"></div><div class="tintcloud-img tintcloud-img-' + this.defaults.skin + ' tintcloud-img-' + this.defaults.style + '"><div class="tc-img-title"><div class="tc-img-title-text">' + this.defaults.title + '</div><div class="tc-img-title-close" onclick="javascript:{ tchUpload.close(); }">×</div></div><div class="tc-img-content"><div class="tc-img-content-select"><div class="tc-img-canvas"><div id="tcImgCanvas"></div></div></div><div class="tc-img-content-form"><input type="file" name="tcImgFile" id="tcImgFile" onchange="javascript:{ tchUpload.loadImg(); }" hidden=""><a href="javascript:void(0)" class="form-button form-upload" onclick="javascript:{ document.getElementById(\'tcImgFile\').click(); }" title="选择图片"><img src="' + this.defaults.upload_icon + '" alt="选择"><span>选择图片</span></a><div class="form-button-group4"><a href="javascript:void(0)" class="form-button form-left" title="放大" onclick="javascript:{ tchUpload.imgScale(true); }"><img src="' + this.defaults.big_icon + '" alt="大"> </a><a href="javascript:void(0)" class="form-button form-shrink" title="缩小" onclick="javascript:{ tchUpload.imgScale(false); }"><img src="' + this.defaults.small_icon + '" alt="小"> </a></div><div class="form-display"><div class="form-display-panel"><img src="" alt="" id="tcImgResult"> </div></div><div class="form-button-group2"><a href="javascript:void(0)" class="form-button form-save" title="保存" onclick="javascript:{ tchUpload.save(); }"><img src="' + this.defaults.save_icon + '" alt="保存"><span>保存</span></a><a href="javascript:void(0)" class="form-button form-close" title="取消" onclick="javascript:{ tchUpload.close(); }"><img src="' + this.defaults.close_icon + '" alt="取消"><span>取消</span></a></div></div></div></div>';
            var _div = document.createElement('div');
            _div.innerHTML = _html;
            _div.id = 'tintCloudImage';
            document.body.appendChild(_div)
        },
        close: function (save) {
            var elm = document.getElementById('tintCloudImage');
            elm.remove();
            if (this.defaults.onclose) {
                if (!save) {
                    this.defaults.onclose()
                }
            }
        },
        save: function () {
            if (this.defaults.onsave) {
                var img = document.getElementById('tcImgResult');
                img.setAttribute("crossOrigin", 'Anonymous');
                if (img.src.indexOf('data:image') > -1) {
                    this.defaults.onsave(img.src);
                    this.close('save')
                } else {
                    this.error('未加载图片')
                }
            } else {
                this.error('未注册保存事件')
            }
        },
        error: function (msg) {
            if (this.defaults.onerror) {
                this.defaults.onerror(msg)
            } else {
                alert(msg)
            }
        },
        loadImg: function () {
            var files = document.getElementById('tcImgFile').files;
            var file = files[0];
            if (file != undefined) {
                this.removeAllChild('tcImgCanvas');
                var reader = new FileReader();
                reader.readAsBinaryString(file);
                reader.onload = function (f) {
                    var tcImgCanvas = document.getElementById('tcImgCanvas');
                    var src = 'data:' + file.type + ';base64,' + window.btoa(this.result);
                    var img = new Image();
                    img.src = src;
                    img.id = 'tcImgModel';
                    img.onload = function () {
                        tcImgCanvas.appendChild(img);
                        var div = document.createElement('div');
                        div.id = 'tcImgBox';
                        tcImgCanvas.appendChild(div);
                        var canvas = document.createElement('canvas');
                        canvas.id = 'tcImgSelect';
                        var previewCanvas = document.createElement('canvas');
                        previewCanvas.id = 'tcImgPreview';
                        previewCanvas.width = tcImgCanvas.offsetWidth;
                        previewCanvas.height = tcImgCanvas.offsetHeight;
                        previewCanvas.style.display = "none";
                        canvas.onmousedown = tchUpload.drawStart;
                        canvas.onmouseup = tchUpload.drawStop;
                        canvas.onmousemove = tchUpload.drawIng;
                        if (canvas.addEventListener) {
                            canvas.addEventListener('DOMMouseScroll', tchUpload.drawScale, false)
                        }
                        canvas.onmousewheel = tchUpload.drawScale;
                        if (img.width > tcImgCanvas.offsetWidth && img.height > tcImgCanvas.offsetHeight) {
                            canvas.width = tcImgCanvas.offsetWidth - 2;
                            canvas.height = canvas.width;
                            canvas.style.left = '0px';
                            canvas.style.top = '0px'
                        } else if (img.width > img.height && img.width > tcImgCanvas.offsetWidth) {
                            canvas.width = (img.height < tcImgCanvas.offsetHeight ? (img.height - 2) : (img.height / 2));
                            canvas.height = canvas.width;
                            canvas.style.left = '0px';
                            canvas.style.top = '0px'
                        } else if (img.width < img.height) {
                            canvas.width = (img.width < tcImgCanvas.offsetWidth ? (img.width - 2) : (img.width / 2));
                            canvas.height = canvas.width;
                            canvas.style.left = (img.width < tcImgCanvas.offsetWidth ? 0 : (img.width / 4)) + 'px';
                            canvas.style.top = canvas.style.left
                        } else if (img.width > img.height) {
                            canvas.width = (img.height < tcImgCanvas.offsetHeight ? (img.height - 2) : (img.height / 2));
                            canvas.height = canvas.width;
                            canvas.style.left = (img.width < tcImgCanvas.offsetWidth ? 0 : (img.width / 4)) + 'px';
                            canvas.style.top = canvas.style.left
                        } else {
                            canvas.width = (img.width < tcImgCanvas.offsetWidth ? (img.height - 2) : (img.height / 2));
                            canvas.height = canvas.width;
                            canvas.style.left = (img.width < tcImgCanvas.offsetWidth ? 0 : (img.width / 4)) + 'px';
                            canvas.style.top = canvas.style.left
                        }
                        tcImgCanvas.appendChild(canvas);
                        tcImgCanvas.appendChild(previewCanvas);
                        tchUpload.options.canvas = canvas;
                        tchUpload.options.draw2d = canvas.getContext('2d');
                        tchUpload.options.drawView2d = previewCanvas.getContext('2d');
                        tchUpload.options.imgView = img;
                        tchUpload.options.img = img;
                        tchUpload.options.resImgWigth = parseInt(img.width);
                        tchUpload.options.resImgHeight = parseInt(img.height);
                        tchUpload.options.width = parseInt(canvas.width);
                        tchUpload.options.height = parseInt(canvas.height);
                        tchUpload.options.sx = parseInt(canvas.style.left);
                        tchUpload.options.sy = parseInt(canvas.style.top);
                        tchUpload.options.x = 0;
                        tchUpload.options.y = 0;
                        tchUpload.options.rotate = 0;
                        tchUpload.options.rotateNew = 0;
                        tchUpload.options.scale = 1;
                        tchUpload.options.scaleNew = 1;
                        tchUpload.drawImage()
                    }
                }
            }
        },
        imgPreview: function () {
            var tcImgCanvas = document.getElementById('tcImgCanvas');
            var dw = parseInt(this.options.imgView.width * this.options.scale) < this.options.imgView.width ? this.options.imgView.width : parseInt(this.options.resImgWigth * this.options.scale);
            var dh = parseInt(this.options.imgView.height * this.options.scale) < this.options.imgView.height ? this.options.imgView.height : parseInt(this.options.resImgHeight * this.options.scale);
            var ops = {
                sx: 0,
                sy: 0,
                sw: this.options.resImgWigth,
                sh: this.options.resImgHeight,
                dx: this.options.x,
                dy: this.options.y,
                dw: dw,
                dh: dh
            };
            var translate = {
                x: 0,
                y: 0
            };
            this.options.drawView2d.save();
            this.options.drawView2d.clearRect(ops.sx, ops.sy, tcImgCanvas.offsetWidth, tcImgCanvas.offsetHeight);
            this.options.drawView2d.rotate(this.options.rotate * Math.PI / 180);
            this.options.drawView2d.drawImage(this.options.imgView, ops.sx, ops.sy, ops.sw, ops.sh, ops.dx, ops.dy, ops.dw, ops.dh);
            this.options.drawView2d.restore();
            var dataURL = document.getElementById('tcImgPreview').toDataURL("image/png");
            var _imgView = new Image();
            _imgView.src = dataURL;
            _imgView.onload = function () {
                tchUpload.options.img = _imgView;
                tchUpload.drawImage()
            }
        },
        imgRotate: function (state) {
            if (this.options.imgView) {
                var img = document.getElementById('tcImgModel');
                if (state) {
                    tchUpload.options.rotateNew -= 90
                } else {
                    tchUpload.options.rotateNew += 90
                } if (tchUpload.options.rotateNew == 360 || tchUpload.options.rotateNew == -360) {
                    tchUpload.options.rotateNew = 0
                }
                img.style.transform = 'rotate(' + tchUpload.options.rotateNew + 'deg)';
                tchUpload.drawImage()
            } else {
                tchUpload.error("未加载图片")
            }
        },
        imgScale: function (state) {
            if (this.options.imgView) {
                var img = document.getElementById('tcImgModel');
                if (state) {
                    tchUpload.options.scaleNew += 0.1
                } else {
                    var _scaleNew = tchUpload.options.scaleNew - 0.1;
                    if (tchUpload.options.resImgWigth * _scaleNew >= 100) {
                        tchUpload.options.scaleNew -= 0.1
                    }
                } if (tchUpload.options.scaleNew <= 0.1) {
                    tchUpload.options.scaleNew = 0.1
                }
                tchUpload.options.scaleNew = parseFloat(tchUpload.options.scaleNew.toFixed(1));
                img.style.width = parseInt(tchUpload.options.resImgWigth * tchUpload.options.scaleNew) + "px";
                tchUpload.drawImage()
            } else {
                tchUpload.error("未加载图片")
            }
        },
        drawScale: function (event) {
            if (!tchUpload.options.isDown) {
                event = event || window.event;
                var direction = null;
                if (event.wheelDelta) {
                    if (event.wheelDelta > 0) {
                        direction = true
                    }
                    if (event.wheelDelta < 0) {
                        direction = false
                    }
                } else if (event.detail) {
                    if (event.detail > 0) {
                        direction = true
                    }
                    if (event.detail < 0) {
                        direction = false
                    }
                }
                var canvas = tchUpload.options.canvas;
                var left = parseInt(canvas.style.left);
                var top = parseInt(canvas.style.top);
                var panelDiv = document.getElementById('tcImgCanvas');
                var divX = panelDiv.offsetWidth;
                var divY = panelDiv.offsetHeight;
                if (direction) {
                    if (left <= 0 && top <= 0 && left + canvas.width <= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = '0px';
                        canvas.style.top = '0px'
                    } else if (left >= 0 && top >= 0 && left + canvas.width >= divX - 2 && top + canvas.height >= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 2) + 'px';
                        canvas.style.top = (top - 2) + 'px'
                    } else if (left >= 0 && top <= 0 && left + canvas.width >= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 2) + 'px';
                        canvas.style.top = '0px'
                    } else if (left <= 0 && top >= 0 && left + canvas.width <= divX - 2 && top + canvas.height >= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = '0px';
                        canvas.style.top = (top - 2) + 'px'
                    } else if (left <= 0 && top >= 0 && left + canvas.width <= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = '0px';
                        canvas.style.top = (top - 1) + 'px'
                    } else if (left >= 0 && top <= 0 && left + canvas.width <= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 1) + 'px';
                        canvas.style.top = '0px'
                    } else if (left >= 0 && top >= 0 && left + canvas.width >= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 2) + 'px';
                        canvas.style.top = (top - 1) + 'px'
                    } else if (left >= 0 && top >= 0 && left + canvas.width <= divX - 2 && top + canvas.height >= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 1) + 'px';
                        canvas.style.top = (top - 2) + 'px'
                    } else if (left >= 0 && top >= 0 && left + canvas.width <= divX - 2 && top + canvas.height <= divY - 2) {
                        canvas.width += 2;
                        canvas.height = canvas.width;
                        canvas.style.left = (left - 1) + 'px';
                        canvas.style.top = (top - 1) + 'px'
                    }
                } else {
                    canvas.width -= 2;
                    canvas.height -= 2;
                    canvas.style.left = (left + 1) + 'px';
                    canvas.style.top = (top + 1) + 'px'
                }
                tchUpload.options.width = canvas.width;
                tchUpload.options.height = canvas.height;
                tchUpload.options.sx = parseInt(canvas.style.left);
                tchUpload.options.sy = parseInt(canvas.style.top);
                tchUpload.drawImage()
            } else {
                tchUpload.error('缩放和拖动不能同时进行')
            }
        },
        drawStart: function () {
            tchUpload.options.isDown = true
        },
        drawStop: function () {
            tchUpload.options.isDown = false;
            tchUpload.options.mouseX = 0;
            tchUpload.options.mouseY = 0
        },
        drawIng: function (event) {
            var e = event || window.event;
            var panelDiv = document.getElementById('tcImgCanvas');
            var clientX = e.clientX;
            var clientY = e.clientY;
            var divPoint = tchUpload.getOffsetPoint(panelDiv);
            if (tchUpload.options.isDown) {
                var canvasPoint = tchUpload.getOffsetPoint(tchUpload.options.canvas);
                if (!tchUpload.options.mouseX && !tchUpload.options.mouseY) {
                    tchUpload.options.mouseX = clientX - canvasPoint.x;
                    tchUpload.options.mouseY = clientY - canvasPoint.y
                }
                var pointX = clientX - divPoint.x;
                var pointY = clientY - divPoint.y;
                var left = (pointX - tchUpload.options.mouseX);
                var top = (pointY - tchUpload.options.mouseY);
                var right = left + tchUpload.options.canvas.width;
                var bottom = top + tchUpload.options.canvas.height;
                if (left <= 0) {
                    left = 0
                }
                if (right >= panelDiv.offsetWidth) {
                    left = panelDiv.offsetWidth - tchUpload.options.canvas.width - 2
                }
                if (top <= 0) {
                    top = 0
                }
                if (bottom >= panelDiv.offsetHeight) {
                    top = panelDiv.offsetHeight - tchUpload.options.canvas.height - 2
                }
                tchUpload.options.canvas.style.left = left + 'px';
                tchUpload.options.canvas.style.top = top + 'px';
                tchUpload.options.sx = left;
                tchUpload.options.sy = top;
                tchUpload.drawImage()
            }
        },
        drawImage: function () {
            this.options.draw2d.save();
            this.options.draw2d.clearRect(this.options.x, this.options.y, this.options.width, this.options.height);
            if (this.options.rotate != this.options.rotateNew) {
                this.options.rotate = this.options.rotateNew;
                this.imgPreview();
                return
            }
            if (this.options.scale != this.options.scaleNew) {
                this.options.scale = this.options.scaleNew;
                this.imgPreview();
                return
            }
            this.options.draw2d.drawImage(this.options.img, this.options.sx + 1, this.options.sy + 1, this.options.width, this.options.height, this.options.x, this.options.y, this.options.width, this.options.height);
            this.options.draw2d.restore();
            var dataURL = document.getElementById('tcImgSelect').toDataURL("image/png");
            document.getElementById('tcImgResult').src = dataURL
        },
        removeAllChild: function (id) {
            var div = document.getElementById(id);
            while (div.hasChildNodes()) {
                div.removeChild(div.firstChild)
            }
        },
        getOffsetPoint: function (element) {
            var point = {
                x: 0,
                y: 0
            };
            while (element != document.body && element != undefined) {
                point.x += element.offsetLeft;
                point.y += element.offsetTop;
                element = element.offsetParent
            }
            return point
        }
    }
    _global = (function () {
        return this || (0, eval)('this')
    }());
    !('tchUpload' in _global) && (_global.tchUpload = tchUpload)
})(window, document);